package cn.shadow.order.service.producer;

import cn.shadow.msg.TestMessage;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 * @author 冯啸迪
 */
@Service
public class Producer {

    @Autowired
    private StreamBridge streamBridge;

    public Boolean producer(String message){
        Map<String, Object> headers = new HashMap<>();
        TestMessage testMessage = new TestMessage();
        testMessage.setMsg(message);
        Message<TestMessage> msg = new GenericMessage<TestMessage>(testMessage, headers);
        streamBridge.send("producer-out-0", msg);
        return true;
    }

    public Boolean producerB(String message){
        Map<String, Object> headers = new HashMap<>();
        TestMessage testMessage = new TestMessage();
        testMessage.setMsg(message);
        Message<TestMessage> msg = new GenericMessage<TestMessage>(testMessage, headers);
        streamBridge.send("producer-out-1", msg);
        return true;
    }

    public Boolean producerOrderly(String message){
        Map<String, Object> headers = new HashMap<>();
        headers.put(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID, 123);
        Message<TestMessage> msg = new GenericMessage<>(new TestMessage(), headers);
        streamBridge.send("producer-out-2", msg);
        return true;
    }

    public Boolean producerDelay(String message){
        Map<String, Object> headers = new HashMap<>();
        headers.put(MessageConst.PROPERTY_DELAY_TIME_LEVEL,2);
        Message<TestMessage> msg = new GenericMessage<>(new TestMessage(), headers);
        streamBridge.send("producer-out-3", msg);
        return true;
    }

}
